local erqi = fk.CreateSkill{
  name = "investiture__erqi",
}

Fk:loadTranslationTable {
  ["investiture__erqi"] = "二气",
  [":investiture__erqi"] = "出牌阶段限一次，你可以明置两张手牌，然后获得两次可于出牌阶段视为使用【火攻】的次数。" ..
    "若你因之弃置明置牌而造成伤害，你获得一次可于出牌阶段视为使用【水淹七军】的次数。",
  ["#investiture__erqi"] = "二气：可以明置两张手牌，然后获得两次可于出牌阶段视为使用【火攻】的次数",

  ["@@investiture__erqi"] = "二气",
  ["#investiture__erqi&"] = "二气",
  ["@investiture__erqi_canuse"] = "次数",
}

local TuntianUtil = require "packages/tuntian_studio/utility"
local DIY = require "packages/diy_utility/diy_utility"

---@param room Room
---@param name string
---@return number
local getCardFromVoid = function (room, name)
  local card = table.find(room:getCardsByArea(Card.Void), function (id)
    return Fk:getCardById(id).trueName == name and
      Fk:getCardById(id):getMark("@investiture__erqi") == 1
  end)

  if not card then
    card = room:printCard(name).id
  end

  return card
end

erqi:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local cards = {}
  for _, name in ipairs{ "fire_attack", "drowning" } do
    local card = Fk:getCardById(getCardFromVoid(room, TuntianUtil.cardFix(name)))
    room:addCardMark(card, "@@investiture__erqi", 1)
    table.insert(cards, card)
  end

  room:moveCardTo(cards, Card.PlayerSpecial, player, fk.ReasonJustMove, nil, "#investiture__erqi&", true, nil)
end)

erqi:addLoseEffect(function (self, player, is_death)
  local cards = player:getCardIds(Player.Special, "#investiture__erqi&")
  player.room:moveCardTo(cards, Card.Void, nil, fk.ReasonJustMove, nil, nil, false)
end)

erqi:addEffect("active", {
  anim_type = "offensive",
  prompt = "#investiture__erqi",
  can_use = function (self, player)
    if player:usedSkillTimes(erqi.name, Player.HistoryPhase) >= 1 then return false end
    local cards = {}
    for _, id in ipairs(player:getCardIds("h")) do
      if not TuntianUtil.isShown(id) then table.insert(cards, id) end
      if #cards == 2 then return true end
    end
  end,
  target_num = 0,
  card_num = 2,
  target_filter = Util.FalseFunc,
  card_filter = function (self, player, to_select, selected)
    return #selected < 2 and not TuntianUtil.isShown(to_select)
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    DIY.showCards(player, effect.cards)
    if player.dead then return end

    local fire = table.find(player:getCardIds(Player.Special, "#investiture__erqi&"), function (id)
      return Fk:getCardById(id).trueName == "fire_attack"
    end)
    if fire then
      room:addCardMark(Fk:getCardById(fire), "@investiture__erqi_canuse", 2)
    end
  end
})

erqi:addEffect(fk.BeforeCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill("investiture__erqi") then
      local room = player.room
      local use = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
      if use and use.data.card.skillName == "investiture__erqi" then
        for _, move in ipairs(data) do
          if move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if TuntianUtil.isShown(info.cardId) then
                event:setCostData(self, { event = use })
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local use = event:getCostData(self).event ---@class GameEvent.UseCard
    use.data.extra_data = use.data.extra_data or {}
    use.data.extra_data.investiture__erqi = true
  end,
})

erqi:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("investiture__erqi") and player == target then
      local use = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      return use and use.data.extra_data and use.data.extra_data.investiture__erqi
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local water = table.find(player:getCardIds(Player.Special, "#investiture__erqi&"), function (id)
      return Fk:getCardById(id).trueName == "drowning"
    end)
    if water then
      player.room:addCardMark(Fk:getCardById(water), "@investiture__erqi_canuse", 1)
    end
  end
})

erqi:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return data.card:getMark("@@investiture__erqi") == 1
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:removeCardMark(data.card, "@investiture__erqi_canuse", 1)
    local new = Fk:cloneCard(data.card.name)
    new.skillName = "investiture__erqi"
    data.card = new
  end
})

erqi:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if #card.subcards == 0 then
      return card:getMark("@@investiture__erqi") == 1 and
        (card:getMark("@investiture__erqi_canuse") == 0 or player.phase ~= Player.Play)
    else
      for _, id in ipairs(card.subcards) do
        if Fk:getCardById(id):getMark("@@investiture__erqi") == 1 then
          return true
        end
      end
    end
  end,
  prohibit_response = function (self, player, card)
    if #card.subcards == 0 then
      return card:getMark("@@investiture__erqi") == 1 and
        (card:getMark("@investiture__erqi_canuse") == 0 or player.phase ~= Player.Play)
    else
      for _, id in ipairs(card.subcards) do
        if Fk:getCardById(id):getMark("@@investiture__erqi") == 1 then
          return true
        end
      end
    end
  end
})

return erqi